<?php

/**
 * @var $key
 * @var $config
 * @var $IS_REG
 * @var $IS_REG_CODE
 */

include("../WeSql/widget.php");
include("../WeSql/config.php");
include("../WeSql/WeSql.php");
//admin	user	pass	qq	mail	money	ip	time
@$user = $_REQUEST['user'];
@$pass = $_REQUEST['pass'];
@$qq = $_REQUEST['qq'];
@$mail = $_REQUEST['mail'];
@$money = $_REQUEST['money'];
@$code = $_REQUEST['code'];


/** @var 获取时间戳 $time */
$time = time();
$weSql = new WeSql($config);



if(!$IS_REG){
    @$result = [
        "code" => -1,
        "msg" => "未开启注册通道",
        "exception" => "后台未开启注册通道请联系管理员！"
    ];
    exit(json_encode($result,JSON_UNESCAPED_UNICODE));
}
//添加一个注册验证码
if($IS_REG_CODE){

    if(isset($_COOKIE['mcode'])):
        $rcode=$_COOKIE['mcode'];
        $rcode=base64_decode($rcode);
        $rcode=base64_decode($rcode);
        $rcode=base64_decode($rcode);
    else:
        $rcode = "";
    endif;

    if($code!=$rcode) {
        $array = [
            'code' => '-1',
            'msg' => '验证码错误',
            "exception" => ''
        ];
        exit(json_encode($array, JSON_UNESCAPED_UNICODE));
    }
}

if($user!="" and $pass!="" and $qq!="" and $mail!=""){
    if(!$weSql->isSelectField("user",[
        "user" => $user
    ])){
        if (!$weSql->insert("user",[
            "user" => $user,
            "pass" => encrypt($key,$pass),
            "qq" => $qq,
            "mail" => $mail,
            "money" => 0,
            "ip" => getIP(),
            "time" => $time
        ])){
            $code = -1;
            $msg = "注册失败";
            $exception = "传入参数有危险性问题";
        }else{
            //注册成功这里默认给cookie清楚了
            setcookie('mcode', '', time() - 60, '/');
            $code = 1;
            $msg = "注册成功";
            $exception = "注册成功";
        }
    }else{
        $code = -1;
        $msg = "账号已经存在";
        $exception = "数据库里信息记录该账号已经存在";
    }
}else{
    $code = -1;
    $msg = "不能留空";
    $exception = "出现了参数为空的情况！";
}
@$result = [
    "code" => $code,
    "msg" => $msg,
    "exception" => $exception
];
echo json_encode($result,JSON_UNESCAPED_UNICODE);

